package com.menker.app.common;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.jude.utils.JUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

/**
 * @author J.Huang
 * @version 1.0
 */

public class Utils {
    public static class UI {
        public static void showToastShort(String text) {
            JUtils.Toast(text);
        }

        public static void showToastLong(String text) {
            JUtils.ToastLong(text);
        }

        public static void closeInputMethod(Activity activity) {
//            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            JUtils.closeInputMethod(activity);
        }

        private static long lastClickTime;

        public static boolean isFastDoubleClick() {
            long time = System.currentTimeMillis();
            long timeDiff = time - lastClickTime;
            if (timeDiff > 0 && timeDiff < 500) {
                return true;
            }
            lastClickTime = time;
            return false;
        }

        public static boolean isFastSwipe() {
            long time = System.currentTimeMillis();
            long timeDiff = time - lastClickTime;
            if (timeDiff > 0 && timeDiff < 800) {
                return true;
            }
            lastClickTime = time;
            return false;
        }
    }

    public static class Network {
        public static boolean isNetworkAvailable() {
            return JUtils.isNetWorkAvilable();
        }
    }

    public static class App {
        public static int getAppVersionCode() {
            return JUtils.getAppVersionCode();
        }

        public static String getAppVersionName() {
            return JUtils.getAppVersionName();
        }
    }

    public static class Device {
        public static String getDeviceUniqueId(Context context) {
            final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            final String tmDevice, tmSerial, androidId;
            tmDevice = "" + tm.getDeviceId();
            tmSerial = "" + tm.getSimSerialNumber();
            androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

            UUID deviceUuid = new UUID(androidId.hashCode(),
                    ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
            return deviceUuid.toString();
        }
    }

    public static class Color {
        public static String getColorString(String hexColor) {
            if (!TextUtils.isEmpty(hexColor)) {
                if (hexColor.startsWith("#")) {
                    hexColor = hexColor.substring(1);
                }

                if (hexColor.length() != 6 && hexColor.length() != 8) {
                    return "";
                }

                StringBuilder sb = new StringBuilder();
                sb.append("#");
                if (hexColor.length() == 6) {
                    sb.append("FF");
                    sb.append(hexColor);
                } else if (hexColor.length() == 8) {
                    sb.append(hexColor);
                }

                return sb.toString();
            } else {
                return "";
            }
        }
    }

    public static class Image {
        public static byte[] bitmap2Bytes(Bitmap bitmap, boolean paramBoolean) {
            Bitmap localBitmap = Bitmap.createBitmap(120, 120, Bitmap.Config.ARGB_8888);
            Canvas localCanvas = new Canvas(localBitmap);
            int i;
            int j;
            if (bitmap.getHeight() > bitmap.getWidth()) {
                i = bitmap.getWidth();
                j = bitmap.getWidth();
            } else {
                i = bitmap.getHeight();
                j = bitmap.getHeight();
            }
            while (true) {
                localCanvas.drawBitmap(bitmap, new Rect(0, 0, i, j), new Rect(0, 0, 120, 120), null);
                if (paramBoolean)
                    bitmap.recycle();
                ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
                localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
                        localByteArrayOutputStream);
                localBitmap.recycle();
                byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
                try {
                    localByteArrayOutputStream.close();
                    return arrayOfByte;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                i = bitmap.getHeight();
                j = bitmap.getHeight();
            }
        }

        public static byte[] getImageBytesFromUrl(String strUrl) throws IOException {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");   //设置请求方法为GET
            conn.setReadTimeout(5*1000);    //设置请求过时时间为5秒
            InputStream inputStream = conn.getInputStream();   //通过输入流获得图片数据
            return getBytesFromInputStream(inputStream);     //获得图片的二进制数据
        }
    }

    /***
     * 从数据流中获得数据
     */
    public static  byte[] getBytesFromInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
}
